/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Log.Level;
/*     */ import com.zimbra.cs.mailclient.MailConfig.Security;
/*     */ import com.zimbra.cs.mailclient.pop3.Pop3Capabilities;
/*     */ import com.zimbra.cs.mailclient.pop3.Pop3Config;
/*     */ import com.zimbra.cs.mailclient.pop3.Pop3Connection;
/*     */ import com.zimbra.cs.mailclient.util.SSLUtil;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import javax.net.ssl.SSLContext;
/*     */ import junit.framework.TestCase;
/*     */ import org.apache.log4j.BasicConfigurator;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestPop3Client
/*     */   extends TestCase
/*     */ {
/*     */   private Pop3Config config;
/*     */   private Pop3Connection connection;
/*     */   private static final String HOST = "localhost";
/*     */   private static final int PORT = 7110;
/*     */   private static final int SSL_PORT = 7995;
/*     */   private static final String USER = "user1";
/*     */   private static final String PASS = "test123";
/*     */   
/*     */   public void testDown()
/*     */     throws Exception
/*     */   {
/*  48 */     if (this.connection != null) {
/*  49 */       this.connection.close();
/*     */     }
/*  51 */     this.config = null;
/*  52 */     this.connection = null;
/*     */   }
/*     */   
/*     */   public void testLogin() throws Exception {
/*  56 */     login();
/*     */   }
/*     */   
/*     */   public void testPlainAuth() throws Exception {
/*  60 */     connect();
/*  61 */     this.connection.authenticate("test123");
/*     */   }
/*     */   
/*     */   public void testTls() throws Exception {
/*  65 */     this.config = getConfig(false);
/*  66 */     this.config.setSecurity(MailConfig.Security.TLS);
/*  67 */     connect();
/*  68 */     login();
/*     */   }
/*     */   
/*     */   public void testCapabilities() throws Exception {
/*  72 */     connect();
/*  73 */     Pop3Capabilities caps = this.connection.getCapabilities();
/*  74 */     assertNotNull(caps);
/*  75 */     assertTrue(caps.hasCapability("STLS"));
/*  76 */     assertTrue(caps.hasCapability("StLs"));
/*  77 */     assertTrue(caps.hasCapability("IMPLEMENTATION", "ZimbraInc"));
/*  78 */     assertFalse(caps.hasCapability("EXPIRE", "NEVER"));
/*     */     
/*  80 */     login();
/*  81 */     caps = this.connection.getCapabilities();
/*     */     
/*  83 */     assertTrue(caps.hasCapability("EXPIRE", "NEVER"));
/*     */   }
/*     */   
/*     */   public void testGetMessageSizes() throws Exception {
/*  87 */     login();
/*  88 */     Integer[] sizes = this.connection.getMessageSizes();
/*  89 */     assertNotNull(sizes);
/*  90 */     assertEquals(this.connection.getMessageCount(), sizes.length);
/*  91 */     for (int i = 0; i < sizes.length; i++) {
/*  92 */       assertEquals(sizes[i], Integer.valueOf(this.connection.getMessageSize(i + 1)));
/*     */     }
/*     */   }
/*     */   
/*     */   public void testGetMessageUids() throws Exception {
/*  97 */     login();
/*  98 */     String[] uids = this.connection.getMessageUids();
/*  99 */     assertNotNull(uids);
/* 100 */     assertEquals(this.connection.getMessageCount(), uids.length);
/* 101 */     for (int i = 0; i < uids.length; i++) {
/* 102 */       assertEquals(uids[i], this.connection.getMessageUid(i + 1));
/*     */     }
/*     */   }
/*     */   
/*     */   public void testGetMessage() throws Exception {
/* 107 */     login();
/* 108 */     for (int msgno = 5; msgno < 20; msgno++) {
/* 109 */       int size = this.connection.getMessageSize(msgno);
/* 110 */       assertTrue(size > 0);
/* 111 */       InputStream is = this.connection.getMessage(msgno);
/* 112 */       assertNotNull(is);
/* 113 */       int count = countBytes(is);
/* 114 */       assertEquals(size, count);
/* 115 */       is.close();
/*     */     }
/*     */   }
/*     */   
/*     */   public void testDeleteMessage() throws Exception {
/* 120 */     login();
/* 121 */     boolean deleted = this.connection.deleteMessage(1);
/* 122 */     assertTrue(deleted);
/* 123 */     int count = this.connection.getMessageCount() - 1;
/* 124 */     this.connection.quit();
/* 125 */     login();
/* 126 */     int newCount = this.connection.getMessageCount();
/* 127 */     assertEquals(count, newCount);
/*     */   }
/*     */   
/*     */   private static int countBytes(InputStream is) throws IOException {
/* 131 */     int count = 0;
/* 132 */     while (is.read() != -1) {
/* 133 */       count++;
/*     */     }
/* 135 */     return count;
/*     */   }
/*     */   
/*     */   private void login() throws IOException {
/* 139 */     connect();
/* 140 */     this.connection.login("test123");
/*     */   }
/*     */   
/*     */   private void connect() throws IOException {
/* 144 */     connect(false);
/*     */   }
/*     */   
/*     */   private void connect(boolean ssl) throws IOException {
/* 148 */     if (this.config == null) {
/* 149 */       this.config = getConfig(ssl);
/*     */     }
/* 151 */     System.out.println("---------");
/* 152 */     this.connection = new Pop3Connection(this.config);
/* 153 */     this.connection.connect();
/*     */   }
/*     */   
/*     */   private static Pop3Config getConfig(boolean ssl) {
/* 157 */     Pop3Config config = new Pop3Config("localhost");
/* 158 */     if (ssl) config.setSecurity(MailConfig.Security.SSL);
/* 159 */     config.setPort(ssl ? 7995 : 7110);
/* 160 */     config.setSSLSocketFactory(SSLUtil.getDummySSLContext().getSocketFactory());
/* 161 */     config.getLogger().setLevel(Log.Level.trace);
/* 162 */     config.setMechanism("PLAIN");
/* 163 */     config.setAuthenticationId("user1");
/* 164 */     return config;
/*     */   }
/*     */   
/*     */   static {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestPop3Client.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */